package com.flashcash.solu.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.flashcash.solu.R;

/**
 * 选择选项 状态：错误、已选内容，默认
 * Created on 2019/5/2 下午9:49.
 */

public class SelectView extends LinearLayout {


    private TextView top, hint, content, error;
    private LinearLayout item;
    private View defaultLine,errorLine;
    private boolean nullable;//可为空

    public SelectView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.widget_select_view, this);
        top = findViewById(R.id.tv_top);
        hint = findViewById(R.id.tv_hint);
        content = findViewById(R.id.tv_content);
        error = findViewById(R.id.tv_error);
        item = findViewById(R.id.ll_item);
        defaultLine = findViewById(R.id.default_line);
        errorLine = findViewById(R.id.error_line);

        // 获得自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.SelectView);

        nullable = typedArray.getBoolean(R.styleable.SelectView_selectNullable, false);
        top.setText(typedArray.getText(R.styleable.SelectView_selectTopString));
        hint.setText(typedArray.getText(R.styleable.SelectView_selectHintString));
        if (!nullable) {
            error.setText(typedArray.getText(R.styleable.SelectView_selectErrorString));
        }

        typedArray.recycle();

        /*item.setOnClickListener(v -> {
            if (mItemOnClickListener != null) {
                mItemOnClickListener.onClick();
            }
        });*/
    }

    /**
     * 设置内容
     *
     * @param content
     */
    public void setText(String content) {
        Log.i("setText", content);
        if (!TextUtils.isEmpty(content)) {
            this.content.setText(content);
            //已选内容
            top.setVisibility(VISIBLE);
            hint.setVisibility(GONE);
            this.content.setVisibility(VISIBLE);
            error.setVisibility(GONE);
            hint.setSelected(false);
            defaultLine.setVisibility(VISIBLE);
            errorLine.setVisibility(GONE);
        } else {//校验数据正确性
            if (!nullable) {
                checkLegal();
            }
        }
    }

    public void setText(int resId) {
        this.content.setText(resId);
        if (!nullable) {
            checkLegal();
        }
    }

    /**
     * 检验数据正确性
     *
     * @return
     */
    public boolean checkLegal() {
        if (content.getVisibility() == GONE || TextUtils.isEmpty(content.getText())) {
            top.setVisibility(GONE);
            hint.setVisibility(VISIBLE);
            this.content.setVisibility(GONE);
            error.setVisibility(VISIBLE);
            hint.setSelected(true);
            defaultLine.setVisibility(GONE);
            errorLine.setVisibility(VISIBLE);
            return false;
        }
        return true;
    }

    public String getText() {
        return content.getText().toString();
    }

    public TextView getTextView() {
        return content;
    }

    public interface ItemOnClickListener {
        void onClick();
    }

    private ItemOnClickListener mItemOnClickListener;

    public void setItemOnClickListener(ItemOnClickListener itemOnClickListener) {
        mItemOnClickListener = itemOnClickListener;
    }
}
